package com.czl.module_service.viewmodel

import android.text.TextUtils
import com.blankj.utilcode.util.RegexUtils
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.HealthReportBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent

class AddFamilyHealthViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()

    val onBirthdayClick: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.choiceBirthdayEvent.call()
    })

    class UiChangeEvent {
        val choiceBirthdayEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    }
    fun addFamilyHealthyReport(relation: HealthReportBean.FamilyData) {
        if (TextUtils.isEmpty(relation.relativesName) || TextUtils.isEmpty(relation.relativesCard) || TextUtils.isEmpty(relation.relativesPhone) || TextUtils.isEmpty(relation.temperature) ) {
            showErrorToast("请补充带*号的必填内容")
            return
        }
        if (relation.relativesName.length < 2) {
            showErrorToast("姓名不能少于两个字符")
            return
        }
        if (relation.relativesPhone.isNotBlank()) {
            if (!RegexUtils.isMobileSimple(relation.relativesPhone)) {
                showErrorToast("亲属手机号格式错误")
                return
            }
        }
        if (relation.relativesCard.isNotBlank()) {
            if (!RegexUtils.isIDCard18Exact(relation.relativesCard)) {
                showErrorToast("身份证号错误")
                return
            }
        }
        if (TextUtils.isEmpty(relation.temperature)) {
            showErrorToast("请填写体温")
            return
        } else {
            if (relation.temperature.toDoubleOrNull()!! <= 35 || relation.temperature.toDoubleOrNull()!! >= 42) {
                showErrorToast("请填写正确的体温")
                return
            }
        }
        if (relation.healthyType == 2) {
            if (relation.suspected.isEmpty()) {
                showErrorToast("请选择疑似症状")
                return
            }
        }
        if (relation.touching == 1) {
            if (TextUtils.isEmpty(relation.touchingTime) || TextUtils.isEmpty(relation.touchingName) || TextUtils.isEmpty(relation.touchingPhone)) {
                showErrorToast("请补充接触者信息")
                return
            }
            if (relation.touchingPhone.isNotBlank()) {
                if (!RegexUtils.isMobileSimple(relation.touchingPhone)) {
                    showErrorToast("接触人手机号格式错误")
                    return
                }
            }
        }
        LiveBusCenter.postAddRelationHealthyEvent(relation)
        finish()
    }
}